<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * approval actions class
 *
 * @author jahid
 */
class approvalActions extends sfActions {

    public function executeRequest(sfWebRequest $request) {
        $research = ResearchTable::getInstance()->find($request->getParameter('researchid'));

        $formParam = explode('&', $request->getParameter('requestForm'));
        $profParam = explode('=', $formParam[0]);

        $prof = EntityTable::getInstance()->find($profParam[1]);
        $this->forward404Unless($prof, 'No professor selected');

        $research->updateApproval(Types::$RESEARCH_APPROVAL_STATUS[Types::$APPR_AWAITING], 
                array("profid" => $prof->id));

        $this->research = $research;

        $this->postAction(Types::$APPROVAL_ACTION_REQUEST, $research);
    }

    public function executeReview(sfWebRequest $request) {
        $research = ResearchTable::getInstance()->find($request->getParameter('researchid'));

        $research->updateApproval(
                Types::$RESEARCH_APPROVAL_STATUS[Types::$APPR_REVIEWING]
        );

        $this->research = $research;

        $this->postAction(Types::$APPROVAL_ACTION_REVIEW, $research);

        $this->setTemplate('request');
    }

    public function executePublish(sfWebRequest $request) {
        $research = ResearchTable::getInstance()->find($request->getParameter('researchid'));

        $research->updateApproval(
                Types::$RESEARCH_APPROVAL_STATUS[Types::$APPR_APPROVED]
        );

        $this->research = $research;

        $this->postAction(Types::$APPROVAL_ACTION_APPROVE, $research);

        $this->setTemplate('request');
    }

    public function executeRevise(sfWebRequest $request) {
        $research = ResearchTable::getInstance()->find($request->getParameter('researchid'));

        $research->updateApproval(
                Types::$RESEARCH_APPROVAL_STATUS[Types::$APPR_REVISION]
        );

        $this->research = $research;

        $this->postAction(Types::$APPROVAL_ACTION_REJECT, $research);

        $this->setTemplate('request');
    }

    public function preAction() {

    }

    public function postAction($action, $research) {
        $user = $this->getUser()->getAttribute('currentuser');

        // send email
        $subject = "RBIS Team sent you a message";
        $to;
        $body;
        $researchUrl = 'http://rbis.kaust.edu.sa' . $this->getController()->genUrl('data/editResearch?id='.$research->id);

        if ($action == Types::$APPROVAL_ACTION_REQUEST) {
            // email from user to prof
            $to = EntityTable::getInstance()->find($research->approvalauthorityid)->getEmail();
            $body =  $user->firstname .' '. $user->lastname . ' sent you a request for his research approval. ' .
                'Click this url to review: ' . $researchUrl;

        } else if ($action == Types::$APPROVAL_ACTION_REVIEW) {
            // email from prof to user
            $to = EntityTable::getInstance()->find($research->entityid)->getEmail();
            $body =  $user->firstname .' '. $user->lastname . ' is reviewing your research. ' .
                'Check this update here: ' . $researchUrl;

        } else if ($action == Types::$APPROVAL_ACTION_APPROVE) {
            // email from prof to user
            $to = EntityTable::getInstance()->find($research->entityid)->getEmail();
            $body =  'Congratulation. ' . $user->firstname .' '. $user->lastname . ' has approved your research. ' .
                'Check this update ' . $researchUrl;

        } else if ($action == Types::$APPROVAL_ACTION_REJECT) {
            // email from prof to user
            $to = EntityTable::getInstance()->find($research->entityid)->getEmail();
            $body =  'Sorry. ' . $user->firstname .' '. $user->lastname . ' aksed to make revision to your research. ' .
                'See your research ' . $researchUrl;
        }

        $this->getLogger()->debug('to: ' . $to);
        $this->getLogger()->debug('body: ' . $body);

        RBISUtility::sendEmail($to, $subject, $body, $user->id);

    }

    public function executeProflist(sfWebRequest $request) {
        $this->proflist = Doctrine::getTable('Entity')->findByDql('entitytyperefid = ?', Types::$ENT_TYPE[Types::$ENT_TYPE_PROFESSOR]);
    }
}

